/**
 * @author lteng
 * @time 2022-06-18 15:06:38 
 */

package liteng.study.cn.stuscore.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import liteng.study.cn.stuscore.comm.Page;
import liteng.study.cn.stuscore.entity.EProject;
import liteng.study.cn.stuscore.entity.EType;
import liteng.study.cn.stuscore.entity.Exam;
import liteng.study.cn.stuscore.entity.Grade;
import liteng.study.cn.stuscore.service.EProjectService;
import liteng.study.cn.stuscore.service.ETypeService;
import liteng.study.cn.stuscore.service.ExamService;
import liteng.study.cn.stuscore.service.GradeService;
import liteng.study.cn.stuscore.service.StudentService;
import liteng.study.cn.stuscore.service.impl.EProjectServiceImpl;
import liteng.study.cn.stuscore.service.impl.ETypeServiceImpl;
import liteng.study.cn.stuscore.service.impl.ExamServiceImpl;
import liteng.study.cn.stuscore.service.impl.GradeServiceImpl;
import liteng.study.cn.stuscore.service.impl.StudentServiceImpl;

/**
 * 考试信息请求控制器
 */
@WebServlet("/exam")
public class ExamServlet extends HttpServlet {

	private ExamService examService = new ExamServiceImpl();
	private ETypeService eTypeService = new ETypeServiceImpl();
	private EProjectService eProjectService = new EProjectServiceImpl();
	private GradeService gradeService = new GradeServiceImpl();

	private StudentService studentService = new StudentServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("exam-get启动！！");
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");

		if (action.equals("page")) {
			pageInfo(request, response);

		} else if (action.equals("add")) {
			addInfo(request, response);

		} else if (action.equals("upd")) {
			updInfo(request, response);

		} else if (action.equals("del")) {
			delInfo(request, response);
		} else if (action.equals("addView")) {
			openAddView(request, response);
		} else if (action.equals("updView")) {
			openUpdView(request, response);

		} else if (action.equals("scoreView")) {
			openScoreView(request, response);

		} else if (action.equals("asc")) {
			openAscView(request, response);
		} else if (action.equals("desc")) {
			openDescView(request, response);
		}
	}

	/**
	 * 
	 * @param request
	 * @param response
	 */
	private void openDescView(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub

	}

	/**
	 * 
	 * @param request
	 * @param response
	 */
	private void openAscView(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub

	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void openUpdView(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id = Integer.valueOf(request.getParameter("id"));

		Exam exam = examService.getOne(id);
		List<EProject> projects = eProjectService.getEProjects();
		List<EType> types = eTypeService.getETypes();
		List<Grade> grades = gradeService.getGrades();

		request.setAttribute("projects", projects);
		request.setAttribute("grades", grades);
		request.setAttribute("types", types);
		request.setAttribute("exam", exam);

		request.getRequestDispatcher("pages/exam/upd.jsp").forward(request, response);

	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void openAddView(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<EProject> projects = eProjectService.getEProjects();
		List<EType> types = eTypeService.getETypes();
		List<Grade> grades = gradeService.getGrades();

		request.setAttribute("projects", projects);
		request.setAttribute("grades", grades);
		request.setAttribute("types", types);

		request.getRequestDispatcher("pages/exam/add.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("exam-post启动！！");
		doGet(request, response);
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int id = Integer.valueOf(request.getParameter("id"));

		Exam exam = examService.getOne(id);
		System.out.println(id);

		examService.delete(exam);

		response.sendRedirect("/stuscore/exam?action=page&pageIndex=1&pageSize=10");

	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void updInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		String intro = request.getParameter("intro");
		int id = Integer.valueOf(request.getParameter("id"));
		int etid = Integer.valueOf(request.getParameter("etid"));
		int epid = Integer.valueOf(request.getParameter("epid"));
		int gid = Integer.valueOf(request.getParameter("gid"));

		Exam exam = examService.getOne(id);

		exam.setName(name);
		exam.setIntro(intro);
		exam.setEpid(epid);
		exam.setEtid(etid);
		exam.setGid(gid);

		examService.update(exam);

		response.sendRedirect("/stuscore/exam?action=page&pageIndex=1&pageSize=10");

	}

	/**
	 * 传入：pageIndex，pageSize 获取page 跳转list
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void pageInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		int pageIndex = Integer.valueOf(request.getParameter("pageIndex"));
		int pageSize = Integer.valueOf(request.getParameter("pageSize"));
//		String name = request.getParameter("name");

		String name = request.getParameter("name");
		if (name != null) {
			name = URLDecoder.decode(name, "utf-8");
		}

		Page page = null;
		if (name != null && !name.equals("")) {
			page = examService.getPageExams(pageIndex, pageSize, name);

		} else {
			page = examService.getPageExams(pageIndex, pageSize);

		}
		request.setAttribute("page", page);

		request.getRequestDispatcher("/pages/exam/list.jsp").forward(request, response);

	}

	/**
	 * 修改年级信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	@SuppressWarnings("unused")
	private void descInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		int id = Integer.valueOf(request.getParameter("id"));

		EType eType = eTypeService.getOne(id);
		request.setAttribute("type", eType);
		request.getRequestDispatcher("/pages/exam/upd.jsp").forward(request, response);

	}

	/**
	 * 传入anme,intro,etid,epid,gid 添加信息 返回1-10 page
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String intro = request.getParameter("intro");
//		int id = Integer.valueOf(request.getParameter("id"));
		int etid = Integer.valueOf(request.getParameter("etid"));
		int epid = Integer.valueOf(request.getParameter("epid"));
		int gid = Integer.valueOf(request.getParameter("gid"));

		Exam exam = new Exam();
//		System.out.println(name);
		exam.setName(name);
		exam.setIntro(intro);
		exam.setEpid(epid);
		exam.setEtid(etid);
		exam.setGid(gid);

		examService.add(exam);
		response.sendRedirect("/stuscore/exam?action=page&pageIndex=1&pageSize=10");

	}

	/**
	 * 传入gid，eid (name) 获取exam,student 跳转score页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void openScoreView(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int gId = Integer.valueOf(request.getParameter("gid"));
		int eId = Integer.valueOf(request.getParameter("eid"));
		String name = request.getParameter("name");

		Exam exam = examService.getOne(eId);
		List<Map<String, Object>> students = null;
		String pName = eProjectService.getOne(exam.getEpid()).getName();

		if (name != null && !name.equals("")) {

			students = studentService.getStudentsWithGradeAndScore(gId, eId, name);
//			request.setAttribute("exam", exam);
//			request.setAttribute("students", students);
		} else {
//			Exam exam = examService.getOne(eId);
			students = studentService.getStudentsWithGradeAndScore(gId, eId);

		}
		
		request.setAttribute("pname", pName);
		request.setAttribute("exam", exam);
		request.setAttribute("students", students);
		request.getRequestDispatcher("/pages/exam/score.jsp").forward(request, response);

	}

}
